package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private WmMaterialMapper wmMaterialMapper;
    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 自媒体素材上传功能
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) throws IOException {
        //1.参数校验（校验参数是否存在）
        if (multipartFile == null || multipartFile.getSize()==0){
            //参数不存在，报出异常
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2.上传图片到minio中存储
        //2.1获取素材名称
        String originalFilename = multipartFile.getOriginalFilename();
        //2.2保证素材名称存储的唯一性
        String filename = UUID.randomUUID().toString().replace("-", "");
        //2.3获取素材的后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //2.4拼接文件名称
        filename = filename + suffix;
        //2.5上传到minio，并返回访问路径
        String url = fileStorageService.uploadImgFile("", filename, multipartFile.getInputStream());

        //3.将上传后的访问路径保存到数据
        //3.1创建对象，封装数据
        WmMaterial material = new WmMaterial();
        material.setUserId(WmThreadLocalUtil.getUser().getId());
        material.setUrl(url);
        material.setType((short) 0);
        material.setIsCollection((short) 0);
        material.setCreatedTime(new Date());
        //3.2保存到数据库中
        wmMaterialMapper.insert(material);
        //4.返回结果
        return ResponseResult.okResult(material);
    }

    /**
     * 素材列表查询
     * @param wmMaterialDto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto wmMaterialDto) {
        //1.检查参数
        if (wmMaterialDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.创建查询对象，封装查询条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        //2.1查询当前登录用户的素材
        queryWrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());
        //2.2添加是否是查询收藏的素材
        if (wmMaterialDto.getIsCollection() != null && wmMaterialDto.getIsCollection() ==1){
            //是查询收藏：封装收藏的查询条件
            queryWrapper.eq(WmMaterial::getIsCollection,wmMaterialDto.getIsCollection());
        }
        //3.执行分页查询
        Page<WmMaterial> page = wmMaterialMapper.selectPage(new Page<>(wmMaterialDto.getPage(), wmMaterialDto.getSize()), queryWrapper);
        //4.封装返回结果
        PageResponseResult result = new PageResponseResult(wmMaterialDto.getPage(), wmMaterialDto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());

        return result;
    }
}
